package com.frogs.ucenter.controller;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.util.DWZResponse;
import com.frogs.framework.util.PageList;
import com.frogs.framework.util.RenderUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.system.util.UserBaseStatusEnum;
import com.frogs.ucenter.entity.UserTaskRecord;
import com.frogs.ucenter.service.UserTaskRecordService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/admin/userTaskRecord")
public class UserTaskRecordController extends BaseController3 {
    private static final String ROOT_VIEW = "admin/userTaskRecord";

    private static final String HOME_VIEW = "admin/userTaskRecord/home";

    private static final String EDIT_VIEW = "admin/userTaskRecord/edit";
    
    private static final String MAIL_RESEND = "/resend";
    

    @Autowired
    private UserTaskRecordService userTaskRecordService;

    @RequestMapping()
    public String listUserTaskRecord(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, true );
        getPageList( model );
        return HOME_VIEW;
    }

    @RequestMapping(EDIT)
    public String createUserTaskRecord(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, false );
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.put( "model",userTaskRecordService.getById( id ) ); 
        }
        return EDIT_VIEW;
    }

    @RequestMapping(ADD)
    public String saveUserTaskRecord(HttpServletRequest request, HttpServletResponse response, ModelMap model, UserTaskRecord userTaskRecord) {
        setRequestModelMap(request, model, false);
        DWZResponse.Builder builder;
        try {
            userTaskRecordService.insert( userTaskRecord ); 
            builder = DWZResponse.getSucessBuilder("新增成功");
        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("新增失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        return null;
    }

    @RequestMapping(SHOW)
    public String showUserTaskRecord(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.addAttribute( userTaskRecordService.getById( id ) ); 
        }
        setRequestModelMap(request, model);
        return null;
    }

    @RequestMapping(UPDATE)
    public String updateUserTaskRecord(HttpServletRequest request, HttpServletResponse response, ModelMap model, UserTaskRecord userTaskRecordNew) throws Exception {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            DWZResponse.Builder builder;
            try {
                UserTaskRecord userTaskRecord = userTaskRecordService.getById( id ); 
                PropertyUtils.copyProperties(userTaskRecord, userTaskRecordNew);
                userTaskRecordService.update( userTaskRecord ); 
                model.clear();
                builder = DWZResponse.getSucessBuilder("更新成功");
            } catch (Exception e) {
                builder = DWZResponse.getFailBuilder("更新失败" + e.getMessage());
            }
            RenderUtil.renderHtml(builder.build().toString(), response);
        }
        return null;
    }

    @RequestMapping(DELETE)
    public String deleteUserTaskRecord(HttpServletRequest request, HttpServletResponse response, ModelMap model, @RequestParam(required=true, value="ids")String inIdList) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap(request, model);
        DWZResponse.Builder builder;
        try {
            String[] idArray = inIdList.split(",");
            model.put("inIdList", idArray);
            userTaskRecordService.delete( model ); 
            builder = DWZResponse.getSucessBuilder("删除信息成功");
        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("删除信息失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        model.clear();
        return null;
    }
    
    @RequestMapping(MAIL_RESEND)
    public String resendMail(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            DWZResponse.Builder builder;
            try {
            	userTaskRecordService.delete(id);
                userTaskRecordService.resendMail(id);
                model.clear();
                builder = DWZResponse.getSucessBuilder("补发成功");
            } catch (Exception e) {
                builder = DWZResponse.getFailBuilder("补发失败" + e.getMessage());
            }
            RenderUtil.renderHtml(builder.build().toString(), response);
        }
        return null;
    }

    private PageList<UserTaskRecord> getPageList(ModelMap model) {
    	model.put("orderByClause", " create_time desc " );
        PageList<UserTaskRecord> userTaskRecordList = userTaskRecordService.getList( model, Integer.parseInt(model.get("pageNum").toString()), Integer.parseInt(model.get("numPerPage").toString()) ); 
        model.put("userTaskRecordList",userTaskRecordList);
        model.put("pageTurn",userTaskRecordList.getPageTurn());
        return userTaskRecordList;
    }
    
}